-- check_main_target_arr
--
local import = import
---@type behavior_ret
local bret = import("bt/bt_plugin/behavior3/behavior_ret")

local M = {
    name = "check_main_target_arr",
    type = "Condition",
    desc = "检查目标列表",
    args = {
        { "targetName", "string", "目标名字" },
        { "minIndex", "int?", "最小索引" },
        { "maxIndex", "int?", "最大索引" },
    },
    output = { "找到的目标数据?" }
}

function M.run(node, env, value)
    local owner = env.owner
    local args = node.args
    local minIndex = args.minIndex
    local maxIndex = args.maxIndex
    local targetName = args.targetName
    if owner.curMainTargetArr and #owner.curMainTargetArr > 0 then
        local count = #owner.curMainTargetArr
        if not minIndex or minIndex < 1 then
            minIndex = 1
        end
        if not maxIndex or maxIndex > count then
            maxIndex = count
        end

        for i = minIndex, maxIndex do
            local tmpMainTarget = owner.curMainTargetArr[i]
            if tmpMainTarget.mainTarget == targetName then
                return bret.SUCCESS, tmpMainTarget
            end
        end
    end


    return bret.FAIL
end

return M
